const date = new Date();
const nowYear = date.getFullYear();
const nowMonth = date.getMonth() + 1;
const nowDay = date.getDate();
var years = [];
console.log(date.getMonth());
console.log(nowDay);
console.log(date.getHours())
console.log(date.getMinutes());
//判断两个时间点的大小
function compareTimePOint(a,b){
  //a,b  [8,52]即8:52
  //a被比较时间，b为比较时间；即b=[12,0]/[0,0]/[20,0]
  var time1 = a[0] * 60 + a[1]
  var time2 = b[0] * 60 + b[1]
  if (time1 > time2){
    return true
  }else{
    return false
  }
}

for (var i = date.getFullYear(); i <= date.getFullYear()+3; i++) {
  years.push(i)//年份数组
}
function stringDate(day){
  if (day<10){
    day='0'+day
  }else{
    day+='';
  }
  return day
}
Component({
  data: {
    years: years,//年份数组
    year: nowYear,
    months: [],//月份数组
    month: nowMonth,
    days: [],//天数数组
    day: nowDay,
    meridiems: ["上午","下午"],
    meridiem: "上午",//显示上午还是下午
    value: [1, 2, 3, 1]
  },
  methods:{
    bindChange: function (e) {
      const val = e.detail.value;
      console.log('val數組',val);
      let months = [];
      let days=[];
      let index_m=0;
      let index_d=0;
      let year = this.data.years[val[0]];//年份
      let meridiem = this.data.meridiems[val[3]];//上午或下午
      let month = this.data.months[val[1]];//月份
      console.log(month)
      let day = this.data.days[val[2]];//日期
      console.log(day);
      //去重新计算月份、日期、上午或下午的数组
      months = getMonthArr(year);
      console.log('整理后的months', months);
      //判断月份、日期所在的位置
      index_m = months.indexOf(month);
      console.log('月份在新數組的位置', index_m);
      if (index_m == -1) {
        //月份不存在
        month = stringDate(nowMonth)
        index_m = 0
      }
      days = setDays(year, month);
      console.log('整理后的days', days);
      index_d = days.indexOf(day);
      console.log('日期在新數組的位置', index_d);
      if (index_d == -1) {
        //日期不存在
        day = stringDate(nowDay)
        index_d = 0
      }
      
      var newValue = [val[0],index_m,index_d,0];
      console.log('新value值',newValue)
      //value的赋值分别在月份、天数的数组前后，具体原因不清楚，但是自己试验多次，这样才保证了value可以在wxml上显示正常
      this.setData({
        value: newValue  
      })
      this.setData({
        year: year,
        month: month,
        day: day,
        meridiem: meridiem,
        months: months,
        days: days
      })
      this.setData({
        value: newValue
      })
    },
    confirmTime(){
      let {year,month,day,meridiem,months,days} = this.data
      console.log('value:'+year+' '+month+' '+day+ ' '+meridiem)
    }
  },
  ready(){
    var months = getMonthArr(nowYear);
    var days = setDays(nowYear, nowMonth);
    var index_m = months.indexOf(stringDate(nowMonth));
    var index_d = days.indexOf(stringDate(nowDay));
    let meridiem = '';
    let index_ap=0;
    let nowTime = date.getHours() + ':' + date.getMinutes()
    let isPm=compareTimePOint([date.getHours(), date.getMinutes()],[12,0]);
    if (isPm){
      //下午
      meridiem="下午";
      index_ap=1;
    }else{
      meridiem = "上午";
      index_ap = 0;
    }
    console.log('months::'+months)
    this.setData({
      months: months,
      days: days,
      meridiem: meridiem
    })
    this.setData({
      value: [0, index_m, index_d, index_ap]
    })
  },
 
})
function getMonthArr(year){
  var myMonthArr=[];
  var i=1;
  if (year == date.getFullYear()) {
    //当年
    i = nowMonth;
  }
  for (i; i <= 12; i++) {
    if (i < 10) {
      i = '0' + i;
    }else{
      i+=''
    }
    myMonthArr.push(i);//月份数组
  }
  return myMonthArr;
}
// 返回月份的天数
function setDays(selectYear, selectMonth) {
  //月日可能为01,02，03...
  var  num = parseInt(selectMonth);//月份
  var day = 1;
  var temp = [];
  if (selectYear == nowYear && num == nowMonth){
    //当年且当月
    day=nowDay;
  }
  if (num === 1 || num === 3 || num === 5 || num === 7 || num === 8 || num === 10 || num === 12) {
    // 判断31天的月份
    for (var i = day; i <= 31; i++) {
      if (i < 10) {
        i = '0' + i;
      }
      temp.push('' + i);
    }
  } else if (num === 4 || num === 6 || num === 9 || num === 11) { // 判断30天的月份
    for (var i = day; i <= 30; i++) {
      if (i < 10) {
        i = '0' + i;
      }
      temp.push('' + i);
    }
  } else if (num === 2) { // 判断2月份天数
    var year = parseInt(selectYear);
    if (((year % 400 === 0) || (year % 100 !== 0)) && (year % 4 === 0)) {
      for (var i = day; i <= 29; i++) {
        if (i < 10) {
          i = '0' + i;
        }
        temp.push('' + i);
      }
    } else {
      for (var i = day; i <= 28; i++) {
        if (i < 10) {
          i = '0' + i;
        }
        temp.push('' + i);
      }
    }
  }
  return temp;
}
